package cn.buyclss.backend.controller.shop;

import cn.buyclss.backend.entity.support.RespResult;
import cn.buyclss.backend.entity.vo.shop.GoodsIdVO;
import cn.buyclss.backend.entity.vo.shop.GoodsStatusUpdateVO;
import cn.buyclss.backend.entity.vo.shop.GoodsVO;
import cn.buyclss.backend.service.shop.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;

/**
 * @author: Cecurio
 * @create: 2019-03-12 16:43
 **/
@RestController
@RequestMapping("/goods")
@Api(value = "商品管理", description = "商品管理")
public class GoodsController {

    private Logger logger = LoggerFactory.getLogger(GoodsController.class);

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/one")
    @RequiresRoles("shop")
    @ApiOperation(value = "增加一个商品", notes = "增加一个商品. 操作者角色：shop")
    @ApiImplicitParam(paramType = "header", name = "Authorization", required = true)
    public RespResult addOne(@RequestBody GoodsVO goodsVO) {
        logger.info(goodsVO.toString());
        return goodsService.addOne(goodsVO);
    }

    @GetMapping("/my")
    @RequiresRoles("shop")
    @ApiOperation(value = "查看我店铺的商品", notes = "查看我店铺的商品. 操作者角色：shop")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "Authorization", required = true),
        @ApiImplicitParam(paramType = "query", name = "pageIndex", value = "分页：第几页，默认为1", required = true),
        @ApiImplicitParam(paramType = "query", name = "pageSize", value = "分页：每页多少条，默认为10", required = true)
    })
    public RespResult getMyGoods(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return goodsService.getMyGoods(pageIndex, pageSize);
    }


    // 修改商品上架状态
    @PutMapping("/shelf")
    @RequiresRoles("shop")
    @ApiOperation(value = "上架(v.)商品", notes = "上架（v.）商品. 操作者角色：shop")
    @ApiImplicitParam(paramType = "header", name = "Authorization", required = true)
    public RespResult updateOne(@RequestBody GoodsIdVO goodsIdVO) {
        logger.info(goodsIdVO.toString());
        return goodsService.shelfGoodsById(goodsIdVO);
    }


    @GetMapping("/specs")
    @RequiresRoles("shop")
    @ApiOperation(value = "通过商品ID获取对应的所有的商品规格", notes = "通过商品ID获取对应的所有的商品规格. 操作者角色：shop")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "Authorization", required = true, value = "token用户凭证"),
        @ApiImplicitParam(paramType = "query", name = "goodsId", required = true, value = "商品编号")
    })
    public RespResult getShopSpecsByShopId(@RequestParam("goodsId") Integer goodsId) throws InvocationTargetException, IllegalAccessException {
        return goodsService.getGoodsSpecsByGoodsId(goodsId);
    }


}
